package com.example.workassignback.Mapper;

import com.example.workassignback.POJO.Patient;
import com.example.workassignback.POJO.ProcessItem;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.example.workassignback.POJO.Process;


import java.time.LocalDate;
import java.util.Date;
import java.util.List;

@Mapper
public interface PatientMapper {


    @Select("""
            select Process_Items.prId,
                   Process_Items.dId,
                   Process_Items.iId,
                   status,
                   therapyAddress,
                   intervalTime,
                   num,
                   isReport,
                   startTime,
                   name,
                   gender,
                   phone,
                   finishTime,
                   iName
            from Process_Items
                     join Processes on Process_Items.prId = Processes.prId
                     join Items on Items.iId = Process_Items.iId
                     join Doctors on Doctors.dId = Process_Items.dId
                     join Users on Users.uId = Doctors.uId
            where Process_Items.prId = (select prId from Processes where pId = #{pId} and DATE(startTime) = #{currentDate});""")
    List<ProcessItem> getPatientProcessByDate(Integer pId, LocalDate currentDate);


    @Select("select pId from Patients where uId = #{id}")
    Integer getPIdByUId(Integer id);

    @Select("select isReport from Processes where pId = #{pId} and DATE(startTime) = #{currentDate}")
    Boolean isPatientReport(Integer pId, LocalDate currentDate);

    @Update("update Processes set isReport = true where pId = #{pId}")
    Integer patientReport(Integer pId);

    @Select("select * from Patients join Users where Users.uID = Patients.Uid and Users.uId = #{id}")
    Patient getInfoByUId(Integer id);

    @Select("""
            select Process_Items.prId,
                   Process_Items.dId,
                   Process_Items.iId,
                   status,
                   therapyAddress,
                   intervalTime,
                   num,
                   isReport,
                   startTime,
                   name,
                   gender,
                   phone,
                   finishTime,
                   iName
            from Process_Items
                     join Processes on Process_Items.prId = Processes.prId
                     join Items on Items.iId = Process_Items.iId
                     join Doctors on Doctors.dId = Process_Items.dId
                     join Users on Users.uId = Doctors.uId
            where Process_Items.prId in (select prId from Processes where pId = #{pId});""")
    List<ProcessItem> getPatientHistoryProcessInfo(Integer pId);

    @Select("select prId from Processes where pId = #{pId}")
    List<Integer> getPatientHistoryPrId(Integer pId);

    @Update("update Users set uId=#{uId},birthday=#{birthday},phone=#{phone},password=#{password},homeAddress=#{homeAddress} where uId=#{uId}")
    Boolean patientModifyUserInfo(Patient patient);

    @Update("update Patients set contactPerson=#{contactPerson},contactPhone=#{contactPhone},relation=#{relation} where uId=#{uId}")
    Boolean patientModifyPatientInfo(Patient patient);
}
